<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col
            :md="5"
            :sm="15"
          >
            <a-form-item label="报表生成时间">
              <a-date-picker
                v-model="queryParam.reportGenerationTime"
                placeholder="报表生成时间"
              />
            </a-form-item>
          </a-col>
          <a-col
            :md="5"
            :sm="15"
          >
            <a-form-item label="批次号">
              <a-input
                placeholder="请输入"
                v-model="queryParam.batchNo"
              />
            </a-form-item>
          </a-col>
          <a-col
            :md="5"
            :sm="15"
          >
            <a-form-item label="保费总额">
              <a-input
                placeholder="请输入"
                v-model="queryParam.totalInsurancePremium"
              />
            </a-form-item>
          </a-col>
          <a-col
            :md="5"
            :sm="15"
          >
            <a-form-item label="保单号">
              <a-input
                placeholder="请输入"
                v-model="queryParam.lossPolicy"
              />
            </a-form-item>
          </a-col>
          <a-col
            :md="4"
            :sm="24"
          >
            <span class="table-page-search-submitButtons">
              <a-button
                type="primary"
                @click="$refs.table.refresh(true)"
              >查询</a-button>
              <a-button
                style="margin-left: 8px"
                @click="() => queryParam = {}"
              >重置</a-button>
              <span
                style="margin-left:10px"
                @click="upDownFlag=!upDownFlag"
              >
                <a-icon :type="upDownFlag?'up':'down'"></a-icon>
              </span>
            </span>

          </a-col>
        </a-row>
        <a-row
          :gutter="48"
          v-show="upDownFlag"
        >
          <a-col
            :md="5"
            :sm="15"
          >
            <a-form-item label="保单生效时间">
              <a-date-picker
                v-model="queryParam.insuranceStartdate"
                placeholder="保单生效时间"
              />
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <s-table
      size="default"
      ref="table"
      rowKey="roleId"
      :columns="columns"
      :data="loadData"
      defaultSort="createTime"
      :scroll="{x:1150}"
    >

      <span
        slot="id"
        slot-scope="text,record,index"
      >
        {{ index + 1 }}
      </span>
      <span
        slot="policyNo"
        slot-scope="text,record"
      >
        <a-input
          placeholder="请输入"
          :value="text"
          @blur="(e)=>policyNoInputChange(e.target.value,record)"
        />
      </span>
      <span
        slot="effectiveTime"
        slot-scope="text,record"
      >
        <a-date-picker
          placeholder="保单生效时间"
          :value="text"
          @change="(date, dateString)=>effectiveTimeChange( dateString,record) "
        />
      </span>
      <span
        slot="action"
        slot-scope="text, record"
      >
        <a @click="downLoad(record)">下载</a>
      </span>
    </s-table>
  </a-card>
</template>

<script>
// 保单号管理界面
import { STable } from '@/components'
import { delRole, changRoleStatus, getSysInsuranceDetailsList, updateLossPolicyAndInsuranceStartdate } from '@/api/system'
import RoleModal from '../modules/RoleModal.vue'
import RoleScopeModal from '../modules/RoleScopeModal.vue'
import pick from 'lodash.pick'
import { checkPermission } from '@/utils/permissions'
import { exportExcel } from '@/utils/download'
export default {
  name: 'SysInsuranceDetailsList2',
  components: {
    STable,
    RoleModal,
    RoleScopeModal
  },
  data () {
    return {
      upDownFlag: false,
      description: '列表使用场景：后台管理中的权限管理以及角色管理，可用于基于 RBAC 设计的角色权限控制，颗粒度细到每一个操作类型。',
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: this.$form.createForm(this),
      mdl: {},
      permissions: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      payTime: '',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'roleId',
          width: 80,
          scopedSlots: { customRender: 'id' }
        },
        {
          title: '报表生成时间',
          width: 200,
          dataIndex: 'reportGenerationTime'
        },
        {
          title: '批次号',
          width: 100,
          dataIndex: 'batchNo'
        },
        {
          title: '订单数量',
          width: 100,
          dataIndex: 'orderNum'
        },
        {
          title: '保费总金额',
          width: 150,
          dataIndex: 'totalInsurancePremium'
        },
        {
          title: '保单号',
          width: 100,
          dataIndex: 'lossPolicy',
          scopedSlots: { customRender: 'policyNo' }
        },
        {
          title: '保单生效时间',
          width: 200,
          dataIndex: 'effectiveTime',
          scopedSlots: { customRender: 'effectiveTime' }
        },
        {
          title: '操作',
          width: 120,
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getSysInsuranceDetailsList(Object.assign(parameter, this.queryParam))
      },
      selectedRowKeys: [],
      selectedRows: [],
      addEnable: checkPermission('system:role:add'),
      editEnabel: checkPermission('system:role:edit'),
      removeEnable: checkPermission('system:role:remove')
    }
  },
  created () {
  },
  methods: {
    onSelectChange (selectedRowKeys) {
      console.log('selectedRowKeys changed: ', selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    },
    handleAdd (parentId) {
      this.$refs.modal.add(parentId)
    },
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    handleScope (record) {
      this.$refs.scopemodal.edit(record)
    },
    onChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleOk () {
      this.$refs.table.refresh(true)
      console.log('handleSaveOk')
    },
    delByIds (ids) {
      delRole({ ids: ids.join(',') }).then(res => {
        if (res.code === 0) {
          this.$message.success(`删除成功`)
          this.handleOk()
        } else {
          this.$message.error(res.msg)
        }
        // const difference = new Set(this.selectedRowKeys.filter(x => !new Set(ids).has(x)))
        // this.selectedRowKeys = Array.from(difference)
        this.selectedRowKeys = []
      })
    },
    onChangeStatus (record) {
      record.status = record.status === '0' ? '1' : '0'
      changRoleStatus(pick(record, 'roleId', 'status'))
      // 发送状态到服务器
    },
    exportExcel () {
      exportExcel('/system/sysInsuranceDetails/export', { ids: this.selectedRowKeys.join(',') })
    },
    downLoad (record) {
      exportExcel('/system/sysInsuranceDetails/export', { ids: record.id + '' })
    },
    policyNoInputChange (val, record) {
      const { insuranceStartdate = '', id } = record
      const params = [{
        insuranceStartdate: insuranceStartdate || '',
        id,
        lossPolicy: val
      }]

      updateLossPolicyAndInsuranceStartdate(params).then(res => {
        if (res.code === 0) {
          this.handleOk()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    effectiveTimeChange (val, record) {
      const { lossPolicy = '', id } = record
      updateLossPolicyAndInsuranceStartdate({
        insuranceStartdate: val,
        id,
        lossPolicy: lossPolicy || ''
      }).then(res => {
        if (res.code === 0) {
          this.handleOk()
        } else {
          this.$message.error(res.msg)
        }
      })
    }

  }
}
</script>
